package com.book.lizhi.ui;

import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.book.lizhi.BaseActivity;
import com.book.lizhi.R;
import com.book.lizhi.adapter.ArticleAdapter;
import com.book.lizhi.constants.Constants;
import com.book.lizhi.model.ArticleModel;
import com.book.lizhi.util.AppLog;
import com.book.lizhi.widget.HttpAsyncLayout;
import com.book.lizhi.widget.HttpAsyncLayout.OnHttpAsyncListner;

public class MyFavActivity extends BaseActivity {

	private ListView mListView;
	private ArticleAdapter mStoryAdapter;
	private List<ArticleModel> mArticleModels;

	private HttpAsyncLayout mAsyncLayout;
	private BroadcastReceiver mDeleFavReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			String id = intent.getStringExtra("id");
			for (ArticleModel model : mArticleModels) {
				if (TextUtils.equals(model.id, id)) {
					mArticleModels.remove(model);
					mStoryAdapter.notifyDataSetChanged();
					break;
				}
			}

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_fav);

		Toolbar toolbar = (Toolbar) findViewById(R.id.toobar);
		setSupportActionBar(toolbar);

		ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setTitle("我的收藏");
		actionBar.setDisplayShowHomeEnabled(false);

		initUI();
		addListener();

		LocalBroadcastManager.getInstance(this).registerReceiver(mDeleFavReceiver, new IntentFilter(Constants.DELETE_FAV));
	}

	@Override
	protected void onResume() {
		super.onResume();
		sendLocalRequest(mAsyncLayout);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == android.R.id.home) {
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	private void initUI() {
		mAsyncLayout = (HttpAsyncLayout) findViewById(R.id.library_http_async_layout);
		mListView = (ListView) findViewById(R.id.fav_listview);
		addListener();
	}

	private void addListener() {

		mAsyncLayout.setOnHttpAsyncListner(new OnHttpAsyncListner() {

			@Override
			public void onSuccessListener(String url, String response) {
				mArticleModels = JSON.parseArray(response, ArticleModel.class);
				if (mArticleModels.size() != 0) {
					mStoryAdapter = new ArticleAdapter(MyFavActivity.this, mArticleModels);
					mListView.setAdapter(mStoryAdapter);
					mListView.bringToFront();
				} else {
					mAsyncLayout.showNoDataError();
				}
			}

			@Override
			public void onErrorListener(VolleyError error) {
				AppLog.e("ERROR", error.toString());
			}

			@Override
			public void onRefreshListener() {
			}
		});

		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(MyFavActivity.this, ArticleActivity.class);
				intent.putExtra(Constants.ARTICLE, (ArticleModel) mStoryAdapter.getItem(position));
				startActivity(intent);
			}
		});

	}

}
